<?php
namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;
use Illuminate\Support\Facades\DB;

class UserEx extends CxhshopBase
{

    protected $table = 'ims_cxh_user_ex';
    protected $fillable = [];
    public $timestamps = false;

    /**
     * 获取用户扩展信息
     * @param $memId
     * @param array $field
     * @return array
     */
    public function getUserExInfo($memId, $field = ["*"])
    {
        $query = self::query();
        $exInfo = $query->where('member_id', $memId)->first($field);
        if ($exInfo) {
            $data = $exInfo->toArray();
        } else {
            $data = [];
        }
        return $data;
               var_dump($data);exit;
    }

    /**
     * 获取代理商下区域会员总数
     * params1 int $memberid
     */
    public function getMemberCount($memberid)
    {
        $count = self::where('uex_parentList', 'like', '%' . $memberid . '%')->count();
        return $count;
    }

    /**
     * 获取我的会员
     * params1 int $memberid
     * params2 string $parentList
     * params3 string $page  分页字符串
     * params4 int $type 区分是代理商还是合伙人 1为合伙人 2代理商
     * params5 int $direct 区分直推间推 合伙人时有用 1直推 2间推
     */
    public function getMyMember($member_id, $parentList, $page = '0,0', $type = 1, $direct = 1)
    {
    
        $result = [];
        //查询用户 ',' 个数
        $levelCountSql = "SELECT *,(LENGTH(uex_parentList) - LENGTH(REPLACE (uex_parentList, ',', ''))) AS levelCount
        FROM ims_cxh_user_ex
        WHERE
        member_id = '" . $member_id . "'";
        $levelCount = DB::connection('jmft_cxh')
            ->table('ims_cxh_user_ex as e')
            ->selectRaw("(LENGTH(uex_parentList) - LENGTH(REPLACE (uex_parentList, ',', ''))) AS levelCount")
            ->where('member_id', $member_id)
            ->first();

        $levelCount = $levelCount->levelCount;

        if ($type == 2) {

            $recommend_all = DB::connection('jmft_cxh')->select("select * from
            (SELECT e.uex_level,e.uex_areaName,e.uex_cityNname,e.uex_provinceName, m.cxhname,m.realname, (LENGTH(uex_parentList) - LENGTH(REPLACE (uex_parentList, ',', ''))) AS levelCount
                FROM ims_cxh_user_ex AS e join ims_ewei_shop_member AS m on e.member_id = m.id
                WHERE
                uex_agent = 0 and
                uex_parentList LIKE '" . $parentList . "%'" . ") t where levelCount <= $levelCount+2 and levelCount>$levelCount");

            $result['count'] = count($recommend_all);

            $recommend = DB::connection('jmft_cxh')->select("select * from
            (SELECT e.uex_level,e.uex_areaName,e.uex_cityNname,e.uex_provinceName, m.cxhname,m.realname, (LENGTH(uex_parentList) - LENGTH(REPLACE (uex_parentList, ',', ''))) AS levelCount
                FROM ims_cxh_user_ex AS e join ims_ewei_shop_member AS m on e.member_id = m.id
                WHERE
                uex_agent = 0 and
                uex_parentList LIKE '" . $parentList . "%'" . ") t where levelCount <= $levelCount+2 and levelCount>$levelCount LIMIT $page");
            // dd($page);
            // dd($recommend);
            foreach ($recommend as $key => $value) {

                $value = (array) $value;
                if($value['uex_level'] == 3) {
                    $value['uex_level'] = '高级合伙人';
                } else if($value['uex_level'] == 2) {
                    $value['uex_level'] = '初级合伙人';
                } else if($value['uex_level'] == 1) {
                    $value['uex_level'] = '普通会员';
                }
                if (($levelCount + 1) == $value['levelCount']) {
                    $value['type'] = '直推';
                    $result['list'][] = $value;
                } elseif (($levelCount + 2) == $value['levelCount']) {
                    $value['type'] = '间推';
                    $result['list'][] = $value;
                }

            }

        } else {

            //直推  $levelCount < levelCount < levelCount + 1
            //间推  $levelCount+1 < levelCount < levelCount + 2
            if ($direct == 1) {
                $compute1 = $levelCount + 1;
                $compute2 = $levelCount;
            } else {
                $compute1 = $levelCount + 2;
                $compute2 = $levelCount + 1;
            }

            $recommend_all = DB::connection('jmft_cxh')->select("select * from
            (SELECT e.uex_level,e.uex_areaName,e.uex_cityNname,e.uex_provinceName,m.cxhname,m.realname, (LENGTH(uex_parentList) - LENGTH(REPLACE (uex_parentList, ',', ''))) AS levelCount
            FROM ims_cxh_user_ex AS e join ims_ewei_shop_member AS m on e.member_id = m.id
            WHERE
            uex_agent = 0 and
            uex_parentList LIKE '" . $parentList . "%'" . ") t where levelCount <= $compute1 and levelCount>$compute2");

            $result['count'] = count($recommend_all);

            $recommend = DB::connection('jmft_cxh')->select("select * from
                (SELECT e.uex_level,e.uex_areaName,e.uex_cityNname,e.uex_provinceName, m.cxhname,m.realname, (LENGTH(uex_parentList) - LENGTH(REPLACE (uex_parentList, ',', ''))) AS levelCount
                FROM ims_cxh_user_ex AS e join ims_ewei_shop_member AS m on e.member_id = m.id
                WHERE
                uex_agent = 0 and
                uex_parentList LIKE '" . $parentList . "%'" . ") t where levelCount <= $compute1 and levelCount>$compute2 LIMIT $page");
            foreach ($recommend as $key => $value) {
                $value = (array) $value;
                if($value['uex_level'] == 3) {
                    $value['uex_level'] = '高级合伙人';
                } else if($value['uex_level'] == 2) {
                    $value['uex_level'] = '初级合伙人';
                } else if($value['uex_level'] == 1) {
                    $value['uex_level'] = '普通会员';
                }
                $result['list'][] = $value;
            }

        }

        return $result;
    }

    /**
     * 获取子代理商
     * params1 int $level  当前用户代理商等级
     * params2 int $member_id
     * params3 string $province 省
     * params4 int $city  市
     */
    public function getParentAgent($level, $member_id, $province = '', $city = '')
    {
        if ($level == 1) {

            $agent_city = self::query()->where('uex_agent', 2)->where('uex_cityNname', $city)->first(['uex_cityNname']);
            if ($agent_city) {
                return $agent_city->uex_cityNname;
            } else {
                $agent_province = self::query()->where('uex_agent', 3)->where('uex_provinceName', $province)->first(['uex_provinceName']);
                if ($agent_province) {
                    return $agent_province->uex_provinceName;
                }
            }

        } else if ($level == 2) {

            $agent_province = self::query()->where('uex_agent', 3)->where('uex_provinceName', $province)->first(['uex_provinceName']);
            if ($agent_province) {
                return $agent_province->uex_provinceName;
            }

        }

        return '暂无';
    }

    public function getChildAgent($level, $province, $city, $page, $limit)
    {
        $condition_level = $level - 1;

        if ($level == 3) {
            $list = self::query()->withCertain('member', ['id', 'member_uuid','cxhname'])->offset($page)->limit($limit)->where('uex_agent', $condition_level)->where('uex_provinceName', $province)->get(['member_id','uex_level','uex_agent','uex_areaName','uex_cityNname','uex_provinceName','uex_parentList'])->toArray();
            $count = self::query()->where('uex_agent', $condition_level)->where('uex_provinceName', $province)->count();
        } else if ($level == 2) {
            $list = self::query()->withCertain('member', ['id', 'member_uuid','cxhname'])->offset($page)->limit($limit)->where('uex_agent', $condition_level)->where('uex_cityNname', $city)->get(['member_id','uex_level','uex_agent','uex_areaName','uex_cityNname','uex_provinceName','uex_parentList'])->toArray();
            $count = self::query()->where('uex_agent', $condition_level)->where('uex_cityNname', $city)->count();
        }
        $result['count'] = $count;

        if ($list) {
            foreach ($list as $key => $value) {
                $list[$key]['member'] = $value['member']['member_uuid'];
                $list[$key]['cxhname'] = $value['member']['cxhname'];
                if ($value['uex_agent'] == 2) {
                    $city_name = $value['uex_cityNname'];
                    $list[$key]['child_count'] = self::where('uex_agent', 1)->where('uex_cityNname', $city_name)->count();
                } else if ($value['uex_agent'] == 1) {
                    $parent_member_id = $value['member_id'];
                    $list[$key]['my_member_count'] = $this->getMyMember($value['member_id'], $value['uex_parentList'], '0,0', 2)['count'];
                }
                if($list[$key]['uex_agent'] == 2) {
                    $list[$key]['uex_agent'] = '市代理';
                } else if($list[$key]['uex_agent'] == 1) {
                    $list[$key]['uex_agent'] = '区县代理';
                } 
                $list[$key]['uex_level'] = '高级合伙人';
                unset($list[$key]['member_id']);
                $result['list'][] = $list[$key];
            }
        }

        return $result;

    }

    /**
     * 和member模型一对一关联
     */
    public function member()
    {
        return $this->hasOne('App\Models\Cxhshop\ShopMember', 'id', 'member_id');
    }
}